<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2019/6/8
 * Time: 22:07
 */

namespace app\Module\Index\Controller;

use app\Module\Index\Logic\MessageLogic;
use app\Module\Index\Logic\MessageType;
use app\Module\Index\Model\CircleModel;
use app\Module\Index\Model\CirclePhotoCollectModel;
use app\Module\Index\Model\CirclePhotoModel;
use app\Module\Index\Model\LogCirclePhotoLookModel;

class CirclePhotoCollectController extends CheckAuth
{
    /**
     * 切换收藏状态
     *
     * @verify int $circle_photo_id
     * @return string
     */
    public function toggleCollectAction($circle_photo_id)
    {
        $circlePhotoCollectModel = new CirclePhotoCollectModel();
        $data                    = $circlePhotoCollectModel->row([
            'user_id'         => $this->userId,
            'circle_photo_id' => $circle_photo_id,
        ]);

        $circlePhotoModel = new CirclePhotoModel();
        $circlePhotoData  = $circlePhotoModel->row([
            'id' => $circle_photo_id,
        ]);

        $circleModel = new CircleModel();
        $circleData  = $circleModel->row(['id' => $circlePhotoData['circle_id']]);


        if (empty($data)) {
            $circlePhotoCollectModel->insert([
                'user_id'         => $this->userId,
                'circle_photo_id' => $circle_photo_id,
                'created'         => now(),
            ]);

            $circlePhotoModel->update('collect_num = collect_num+1', [
                'id' => $circle_photo_id,
            ]);

            $messageLogic = new MessageLogic();
            $messageLogic->add($this->userId, $circleData['user_id'], $circle_photo_id, MessageType::CIRCLE_PHOTO_COLLECT, $circlePhotoData['title']);


            return success(true);
        } else {
            $circlePhotoCollectModel->delete([
                'user_id'         => $this->userId,
                'circle_photo_id' => $circle_photo_id,
            ]);

            $circlePhotoModel->addWhere('collect_num', '0', '>');
            $circlePhotoModel->update('collect_num = collect_num-1', [
                'id' => $circle_photo_id,
            ]);

            $messageLogic = new MessageLogic();
            $messageLogic->delete($this->userId, $circleData['user_id'], $circle_photo_id, MessageType::CIRCLE_PHOTO_COLLECT);

            return success(false);
        }
    }

    /**
     * 获取关注状态
     * @verify int $circle_photo_id
     * @return bool
     */
    public function getCollectAction($circle_photo_id)
    {
        $circlePhotoCollectModel = new CirclePhotoCollectModel();
        $data                    = $circlePhotoCollectModel->row([
            'user_id'         => $this->userId,
            'circle_photo_id' => $circle_photo_id,
        ]);

        return success(!empty($data));
    }

    /**
     * 添加圈子照片浏览量
     * @verify int $circle_photo_id
     * @return string
     */
    public function addLookNumAction($circle_photo_id)
    {
        $logCirclePhotoLookModel = new LogCirclePhotoLookModel();
        $logCirclePhotoLookModel->addWhere('created', date("Y-m-d", strtotime("-1 hour")), '>=');
        $logCirclePhotoLookModel->addWhere([
            'circle_photo_id' => $circle_photo_id,
            'user_id'         => $this->userId,
        ]);
        $lookNum = $logCirclePhotoLookModel->count('1', null, 600);
        // 每个用户每小时只能增加一个
        if ($lookNum > 0) {
            return success();
        }

        $logCirclePhotoLookModel->insert([
            'circle_photo_id' => $circle_photo_id,
            'user_id'         => $this->userId,
            'created'         => now(),
        ]);
        $circlePhotoModel = new CirclePhotoModel();
        $circlePhotoModel->update('look_num=look_num+1', ['id' => $circle_photo_id]);
        return success();
    }

    /**
     * 获取收藏照片列表
     *
     * @verify int $page
     * @verify int $limit
     * @return string
     */
    public function getListAction($page = 1, $limit = 10)
    {
        $circlePhotoModel = new CirclePhotoModel();
        $circlePhotoModel->setFields('circle_photo.*');
        $circlePhotoModel->setPage($page, $limit);
        $circlePhotoModel->setJoin('circle_photo_collect', 'circle_photo_collect.circle_photo_id = circle_photo.id');
        $circlePhotoModel->addOrder('circle_photo_collect.created', false);
        $data = $circlePhotoModel->select([
            'circle_photo.is_hide'         => 0,
            'circle_photo_collect.user_id' => $this->userId,
        ]);


        return success($data);
    }
}